#!/bin/bash

BIN_DIR=/usr/local

ping -c2 192.168.2.7 >/dev/null
if [ $? -eq 0 ];then
    SERVER='192.168.2.7'
else
    SERVER='kaifa.hc-yun.com:30027'
fi

function Download() {
    while true; do
        $@ && return
    done
}

Prep(){
    setenforce 0
    sed -i 's#SELINUX=.*#SELINUX=disabled#' /etc/selinux/config
    if [ ! -f /usr/bin/wget ]; then
        rm -f /etc/yum.repos.d/*.repo
        curl -so /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
        curl -so /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
        sed -i '/aliyuncs.com/d' /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/epel-7.repo
        yum install -y wget
    fi
}

Install_Jdk(){
    # install jdk
    which java > /dev/null 2>&1 && { echo 'Jdk installed'; exit 1; }
    Download wget -q -c http://$SERVER/Microservice/jdk-8u171-linux-x64.tar.gz
    tar xf jdk-8u171-linux-x64.tar.gz -C ${BIN_DIR}
    ln -sf ${BIN_DIR}/jdk1.8.0_171 ${BIN_DIR}/java
    if [ $(grep "# jdk env" ~/.bashrc | wc -l) -eq 0 ]; then
        echo -e "\n# jdk env" >>  ~/.bashrc
        echo "JAVA_HOME=${BIN_DIR}/java" >>  ~/.bashrc
        echo 'JRE_HOME=${JAVA_HOME}/jre' >>  ~/.bashrc
        echo 'CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib' >>  ~/.bashrc
        echo 'PATH=$JAVA_HOME/bin:$PATH' >>  ~/.bashrc
        echo 'export PATH JAVA_HOME CLASSPATH' >>  ~/.bashrc
    fi
    source ~/.bashrc && java -version
}

Install_Maven(){
    # install maven
    which mvn > /dev/null 2>&1 && { echo 'Jdk installed'; exit 1; }
    Download wget -q -c http://$SERVER/Microservice/apache-maven-3.6.1-bin.tar.gz
    tar -xf apache-maven-3.6.1-bin.tar.gz -C ${BIN_DIR}
    ln -sf ${BIN_DIR}/apache-maven-3.6.1 ${BIN_DIR}/maven
    if [ $(grep "# maven env" ~/.bashrc | wc -l) -eq 0 ]; then
        echo -e "\n# maven env" >>  ~/.bashrc
        echo "export MAVEN_HOME=${BIN_DIR}/maven" >> ~/.bashrc
        echo 'export PATH=$PATH:$MAVEN_HOME/bin' >> ~/.bashrc
    fi

    line=$(grep -rn '<mirrors>' ${BIN_DIR}/maven/conf/settings.xml | awk -F : '{print $1}')
    sed -i "${line}a\    </mirror>" ${BIN_DIR}/maven/conf/settings.xml
    sed -i "${line}a\        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>" ${BIN_DIR}/maven/conf/settings.xml
    sed -i "${line}a\        <mirrorOf>*,\!maven_nexus_201</mirrorOf>" ${BIN_DIR}/maven/conf/settings.xml
    sed -i "${line}a\        <id>aliyun_nexus</id>" ${BIN_DIR}/maven/conf/settings.xml
    sed -i "${line}a\    <mirror>" ${BIN_DIR}/maven/conf/settings.xml
    line=$(grep -rn '<pluginGroups>' ${BIN_DIR}/maven/conf/settings.xml | awk -F : '{print $1}')
    sed -i "${line}a\      <pluginGroup>com.spotify</pluginGroup>" ${BIN_DIR}/maven/conf/settings.xml
    sed -i '48a\    <localRepository>/var/lib/maven/repo</localRepository>' ${BIN_DIR}/maven/conf/settings.xml

    source ~/.bashrc && mvn -v
}

Install_Node(){
    # install nodejs-10
    which npm > /dev/null 2>&1 && { echo 'Node installed'; exit 1; }
    Download wget -q -c http://$SERVER/Microservice/node-v10.15.3-linux-x64.tar.gz
    tar xf node-v10.15.3-linux-x64.tar.gz -C ${BIN_DIR}
    ln -sf ${BIN_DIR}/node-v10.15.3-linux-x64 ${BIN_DIR}/node
    [ $(grep "# nodejs env" ~/.bashrc | wc -l) -eq 0 ] && echo -e "\n# nodejs env\nexport PATH=${BIN_DIR}/node/bin:\$PATH" >> ~/.bashrc
    source ~/.bashrc && npm -v
    npm config set registry https://registry.npm.taobao.org
}

Install_Yarn(){
    # install yarn
    which yarn > /dev/null 2>&1 && { echo 'Yarn installed'; exit 1; }
    Download wget -q -c http://$SERVER/Microservice/yarn-v1.16.0.tar.gz
    tar xf yarn-v1.16.0.tar.gz -C ${BIN_DIR}
    ln -sf ${BIN_DIR}/yarn-v1.16.0 ${BIN_DIR}/yarn
    [ $(grep "# yarn env" ~/.bashrc | wc -l) -eq 0 ] && echo -e "\n# yarn env\nexport PATH=\$PATH:${BIN_DIR}/yarn/bin" >> ~/.bashrc
    source ~/.bashrc && yarn -v
}

Install_Docker(){
    which docker > /dev/null 2>&1 && { echo 'Docker installed'; exit 1; }
    Download wget -q -c http://$SERVER/Microservice/docker-18.09.5.tgz
    mkdir -p ${BIN_DIR}/docker/bin
    tar xf docker-18.09.5.tgz -C ${BIN_DIR}/docker/bin --strip-components 1
    [ $(grep "# docker env" ~/.bashrc | wc -l) -eq 0 ] && echo -e "\n# docker env\nPATH=${BIN_DIR}/docker/bin:\$PATH" >> ~/.bashrc

    [ -d /etc/docker ] || mkdir /etc/docker
    cat > /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": ["http://3272dd08.m.daocloud.io", "https://docker.mirrors.ustc.edu.cn"],
  "max-concurrent-downloads": 10,
  "log-driver": "json-file",
  "log-level": "warn",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
    },
  "data-root": "/var/lib/docker",
  "hosts": ["tcp://0.0.0.0:2376", "unix:///var/run/docker.sock"]
}
EOF

    cat > /etc/systemd/system/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io
After=keepalived.service glusterd.service autofs.service

[Service]
Type=idle
Environment="PATH=${BIN_DIR}/docker/bin:/bin:/sbin:/usr/bin:/usr/sbin"
ExecStart=${BIN_DIR}/docker/bin/dockerd
ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT
ExecReload=/bin/kill -s HUP $MAINPID
Restart=on-failure
RestartSec=5
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Delegate=yes
KillMode=process

[Install]
WantedBy=multi-user.target
EOF

    iptables -P INPUT ACCEPT
    iptables -F
    iptables -X
    iptables -F -t nat
    iptables -X -t nat
    iptables -F -t raw
    iptables -X -t raw
    iptables -F -t mangle
    iptables -X -t mangle

    systemctl daemon-reload
    systemctl start docker
    systemctl enable docker
    docker info

    Download wget -q -c -O /usr/local/bin/docker-compose http://$SERVER/Microservice/docker-compose
    chmod +x /usr/local/bin/docker-compose
    docker-compose -v
}


case "$1" in
    install)
      Prep
      Install_Jdk
      Install_Maven
      Install_Node
      Install_Yarn
      Install_Docker
      ;;
    uninstall)
      rm -rf ${BIN_DIR}/jdk1.8.0_171 ${BIN_DIR}/java
      rm -rf ${BIN_DIR}/apache-maven-3.6.1 ${BIN_DIR}/maven
      rm -rf ${BIN_DIR}/node-v10.15.3-linux-x64 ${BIN_DIR}/node
      rm -rf ${BIN_DIR}/yarn-v1.16.0 ${BIN_DIR}/yarn
      systemctl disable docker
      rm -f /etc/systemd/system/docker.service
      rm -rf /etc/docker
      rm -rf ${BIN_DIR}/docker
      rm -rf /usr/local/bin/docker-compose
      head -n 12 ~/.bashrc > /tmp/bashrc
      mv /tmp/bashrc ~/.bashrc
      ;;
    *)
      echo "Usage $0 {install|uninstall}"
esac 
